# Credits:
# hyperheart - For coming up with concept and original coding.
from es import getplayerhandle, createentityindexlist, getindexprop, setindexprop, entitysetvalue, fire, precachemodel, doblock
from gamethread import delayed
from superdice.superdice import registerRoll, unregisterRoll, players
from superdice.libs.diceConfigs import configFile
from superdice.libs.playerExtras import getPlayer

def es_map_start(ev):
    cfgData.execute()

def unload():
    unregisterRoll(diceRoll)
    doblock('corelib/noisy_off')

def player_spawn(ev):
    players[ev['userid']]['skull'] = False

def player_hurt(ev):
    attacker = ev['attacker']
    if not attacker in players:
        return
    
    if not players[attacker]['skull'] or int(ev['es_userdead']) or ev['weapon'] != 'hegrenade':
        return

    getPlayer(ev['userid']).burn = burntime

def weapon_fire(ev):
    userid = ev['userid']
    if not players[userid]['skull'] or ev['weapon'] != 'hegrenade':
        return

    delayed(0.2, changeNade, (userid, getplayerhandle(userid)))

def changeNade(userid, handle):
    for index in createentityindexlist('hegrenade_projectile'):
        if not getindexprop(index, 'CBaseEntity.m_hOwnerEntity') == handle:
            continue

        entitysetvalue(index, 'targetname', index)
        setindexprop(index, 'CBaseEntity.m_nModelIndex', precachemodel('models/gibs/agibs.mdl'))
        fire(userid, index, 'ignite')
        fire(userid, index, 'addoutput', 'rendercolor 255 255 255')

def diceRoll(userid, player, eplayer):
    players[userid]['skull'] = True

cfgData = configFile('dice_skullnade')
burntime = cfgData.cvar('superdice_cfg_skullnade_burntime', 3, 'What is the burn time of damaged players?')
cfgData.write()
cfgData.execute()

doblock('corelib/noisy_on')

registerRoll(diceRoll, 'skullnade')
